<?php

namespace app\api\controller;

//任务中心相关操作
use think\Db;
use think\Exception;
use WebGeeker\Validation\Validation;

class Task extends UserBase
{
    //签到提醒
    public function setSignCaution()
    {
        try {
            // 验证（如果验证不通过，会抛出异常)
            Validation::validate($_GET, [
                "is_caution" => "Required|int|IntIn:0,1",
            ]);

            $is_caution = $_GET['is_caution'];

            switch ($this->is_caution) {
                case 0:

                    if ($is_caution == 0) {
                        throw new Exception('非法设置取消提醒');
                    }

                    $msg = '设置提醒';

                    break;

                case 1:

                    if ($is_caution == 1) {
                        throw new Exception('非法设置提醒');
                    }

                    $msg = '设置取消提醒';

                    break;
            }

            $result = Db::name('user')
                ->where(['user_id' => $this->user_id])
                ->update(['is_caution' => $is_caution]);

            if ($result) {
                successBack([], $msg . '成功');
            } else {
                throw new Exception($msg . '失败',500);
            }

        } catch (\Exception $exception) {
            errorBack($exception);
        }
    }

    //签到展示接口
    public function signShowList()
    {
        //签到展示接口
        try {
            //获取配置签到获取到的配置

            //签到配置
            $sign_conf_obj = Db::name('sign_conf');

            //连续签到奖励列表
            $sign_conf_list = $sign_conf_obj->where('day_number', 'BETWEEN', '1,7')->select();


            successBack(['sign_conf_list' => $sign_conf_list], '请求成功');

        } catch (\Exception $exception) {
            errorBack($exception);
        }
    }

    //用户签到
    public function userSign()
    {
        try {
            //user_sign_record用户签到记录表
            Validation::validate($_GET, [
                "day_number" => "Required|int|IntGeLe:1,7",
            ]);

            //第几天签到
            $day_number = $_GET['day_number'];

            //签到表的对象
            $user_sign_record_obj = Db::name('user_sign_record');

            //获取今天的日期
            $today = date('Y-m-d');

            //获取签到请求的时间(年月日时分秒)
            $sign_time = date('Y-m-d H:i:s');

            $yesterday = date("Y-m-d", strtotime("-1 day"));

            //获取今天和昨天有没有签到
            $finish_sign_result = $user_sign_record_obj
                ->where(['sign_date' => ['in', [$today, $yesterday], 'user_id' => $this->user_id]])
                ->order('succession_sign_day desc')
                ->select();

            $add_data = [];

            $add_data['user_id'] = $this->user_id;

            $add_data['sign_time'] = $sign_time;

            $add_data['sign_date'] = $today;

            //如果今天和昨天都没有签到
            if (!$finish_sign_result) {
                //今天和昨天都没有签到
                if ($day_number != 1) {
                    throw new Exception('签到天数不正确');
                }
            } else {
                //今天或昨天有签到数据

                //判断今天是不是已经签到了
                if ($finish_sign_result[0]['sign_date'] == $today) {
                    //今天已经签到
                    throw new Exception('今天已经签到了', 500);
                } else {
                    //今天没有签到

                    //查看用户连续签到了第几天
                    $succession_sign_day = $finish_sign_result[0]['succession_sign_day'];

                    if ($succession_sign_day == 7) {
                        $succession_sign_day = 1;
                    } else {
                        $succession_sign_day++;
                    }

                    if ($succession_sign_day != $day_number) {
                        throw new Exception('签到天数不正确');
                    }

                }
            }

            //签到配置
            $sign_conf = getSignAward($day_number);

            $add_data['succession_sign_day'] = $day_number;

            $add_data['get_feiyanbi_award'] = $sign_conf['feiyanbi_award'];

            $add_data['get_experience_award'] = $sign_conf['experience_award'];

            //将签到的数据插入到数据库
            $sign_result = $user_sign_record_obj->insertGetId($add_data);

            if ($sign_result) {
                successBack([], '签到成功');
            } else {
                throw new Exception('签到失败', 500);
            }

        } catch (\Exception $exception) {
            requestFailReturnBack($exception);
        }
    }


}